package com.tjp.eight.ui.fragment.detail;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Point;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ResultModel;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.constant.ConstantUtils;
import com.tjp.eight.constant.UrlConstant;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.comment.CommentActivity;
import com.tjp.eight.ui.fragment.comment.CommentModel;
import com.tjp.eight.ui.fragment.comment.CommentZanModel;
import com.tjp.eight.ui.fragment.comment.SendCommentPostModel;
import com.tjp.eight.ui.fragment.comment.ZanCommentResultModel;
import com.tjp.eight.ui.fragment.sub.SubItemModel;
import com.tjp.eight.ui.fragment.sub.SubPModel;
import com.tjp.eight.ui.fragment.sub.SubResultModel;
import com.tjp.eight.ui.fragment.zuji.ActivityZuJi;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.AppUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.Cutils;
import com.tjp.eight.utils.DescriptUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.CircleImageView;
import com.tjp.eight.views.DialogLogin;
import com.tjp.eight.views.DialogSendingComment;
import com.tjp.eight.views.LogoutDailog;
import com.tjp.eight.views.SendCommentDialog;
import com.tjp.eight.views.ShareDialog;
import com.tjp.eight.views.emoj.EmojiUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.IOException;
import java.util.ArrayList;


/**
 * Created by Administrator on 2017/2/15.
 */

public class FragmentArticleDetail extends BaseFragment<ArticlePresenter> implements View.OnLayoutChangeListener, DetaisView {
    private String caid = "";
    private LinearLayout ll_history_one;
    private LinearLayout ll_history_two;
    private LinearLayout ll_container_detail_two;
    private RelativeLayout rl_to_comment;
    private RelativeLayout rl_detail_comment_more;
    private ImageView iv_share_detail;
    private ImageView iv_a_detail_title_pic;
    private ImageView ivDetailZan;
    private ImageView ivDetailWeak;
    private RelativeLayout rl_zan_container;
    private RelativeLayout rl_detail_return;
    private RelativeLayout rl_name_de;
    private LinearLayout ll_hot_comment;
    private LinearLayout ll_like_container;
    private LinearLayout ll_more_comment;
    private WebView wvDetail;
    private TextView tv_is_sub;
    private TextView tv_detail_comment_click;
    private TextView tv_detail_zan_count;
    private TextView tv_detail_weak_count;
    private TextView tv_detail_ca_name;
    private TextView tv_hot_comment_tips;
    private TextView tv_com_times;
    private TextView tv_a_detail_title;
    private TextView tv_a_detail_name;
    private TextView tv_a_detail_time;
    private View v_hot_comment;
    private CircleImageView iv_user_p;
    private SendCommentDialog sendCommentDialog;
    private DialogSendingComment dialogSendingComment;
    private LogoutDailog dialogLogin;
    private ShareDialog shareDialog;
    private TextView currentTv;
    private ImageView currentIv;
    private int count = 0;
    private int count2 = 0;
    private int screenHeight = 0;
    private int keyBordHeight = 0;
    private int screenWidth = 0;
    private int currentShareType = -1;
    private String comeFrom = "来自八秒视频";

    private RelativeLayout loadingView;
    private RelativeLayout containerView;
    private ProgressBar progressBar;

    @Override
    protected ArticlePresenter createPresenter() {
        return new ArticlePresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_article_detail, container, false);
        }
        caid = getActivity().getIntent().getStringExtra("caid");
        initViews();
        contentView.addOnLayoutChangeListener(this);
        initHeight();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPostJson());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        wvDetail.loadUrl("about:blank");
        wvDetail.stopLoading();
        wvDetail.setWebChromeClient(null);
        wvDetail.setWebViewClient(null);
        wvDetail.destroy();
        wvDetail = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        wvDetail.onResume();
        wvDetail.resumeTimers();
    }

    @Override
    public void onPause() {
        super.onPause();
        wvDetail.onPause();
        wvDetail.pauseTimers();
    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if (bottom - oldBottom > keyBordHeight) {
            if (sendCommentDialog != null && sendCommentDialog.isShowing() && sendCommentDialog.isHide()) {
                sendCommentDialog.dismiss();
            }
        }
    }

    @Override
    public void loadData(final DetailModel detailModel) {
        if (detailModel == null) return;
        if (detailModel.money_msg != null) {
            ToatUtils.toastCommentSuccess(getContext(), "完成" + detailModel.money_msg.info + "\n获取" + detailModel.money_msg.money + "金币");
            AppUtils.refreshMe();
        }
        if (IsSuccessUtils.isSuccess(detailModel.result)) {
            if (detailModel.data == null) return;
            if (detailModel.data.content != null) {
                Glide.with(getActivity()).load(detailModel.data.content.cover_path).error(R.drawable.loading).into(iv_a_detail_title_pic);
                Glide.with(getActivity()).load(detailModel.data.content.cover_path).error(R.drawable.icon_login).into(iv_user_p);
                tv_detail_weak_count.setText(StringUtils.toWan(detailModel.data.content.unpraise) + "");
                tv_detail_zan_count.setText(StringUtils.toWan(detailModel.data.content.praise) + "");
                tv_detail_ca_name.setText(detailModel.data.content.caid_name + "");
                if (detailModel.data.content.com_times > 99)
                    tv_com_times.setText("99");
                else
                    tv_com_times.setText(detailModel.data.content.com_times + "");
                tv_a_detail_title.setText(detailModel.data.content.title + "");
                if (StringUtils.isNullOrEmpty(detailModel.data.content.come_from)) {
                    tv_a_detail_name.setText("");
                    tv_a_detail_name.setVisibility(View.GONE);
                } else {
                    tv_a_detail_name.setVisibility(View.VISIBLE);
                    tv_a_detail_name.setText(detailModel.data.content.come_from);
                }
                tv_a_detail_time.setText(detailModel.data.content.pass_time + "");
                if (StringUtils.isNullOrEmpty(detailModel.data.content.caid_name)) {
                    rl_name_de.setVisibility(View.GONE);
                } else {
                    rl_name_de.setVisibility(View.VISIBLE);
                }
            }
            initYouLike(detailModel.data.like);
            initHotComment(detailModel.data.commentlist);
            initHistory(detailModel.data.view_history, detailModel.data.content.content_view);
            if (detailModel.data.content.if_subscibe == 1) {
                tv_is_sub.setText("更多热点内容");
                tv_is_sub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.startSubDetails(getActivity(), detailModel.data.content.caid_name, detailModel.data.content.caid);
                    }
                });
            } else {
                tv_is_sub.setText("+订阅");
                tv_is_sub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showSendingDialog();
                        presenter.sub(getSubJson(detailModel.data.content.caid));
                    }
                });
            }
        }
        com.tjp.eight.utils.AnimationUtils.alphaViewGone(loadingView);
        com.tjp.eight.utils.AnimationUtils.alphaViewGone(progressBar);
        com.tjp.eight.utils.AnimationUtils.alphaViewVisible(containerView);
    }

    @Override
    public void loadFail(String msg) {
        ToatUtils.toastCommentFail(getContext(), msg);
    }

    @Override
    public void sendComment(ResultModel resultModel) {
        if (resultModel == null) return;
        if (resultModel.money_msg != null) {
            ToatUtils.toastCommentSuccess(getContext(), "完成" + resultModel.money_msg.info + "\n获取" + resultModel.money_msg.money + "金币");
            AppUtils.refreshMe();
        }
        if (IsSuccessUtils.isSuccess(resultModel.result)) {
            ToatUtils.toastCommentSuccess(getActivity());
            if (sendCommentDialog != null) {
                sendCommentDialog.setEditText("");
            }
        } else {
            ToatUtils.toastCommentFail(getActivity(), resultModel.msg);
        }
    }

    @Override
    public void complite() {
        hideSendingDialog();
    }

    @Override
    public void ZanResult(ZanCommentResultModel model) {
        if (model == null || currentIv == null || currentIv == null) return;
        if (model.money_msg != null) {
            ToatUtils.toastCommentSuccess(getContext(), "完成" + model.money_msg.info + "\n获取" + model.money_msg.money + "金币");
            AppUtils.refreshMe();
        }
        if (IsSuccessUtils.isSuccess(model.result)) {
            currentTv.setText("" + StringUtils.toWan(model.praise));
            if (IsSuccessUtils.isSero(model.do_type)) {
                currentIv.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.like_un));
            } else {
                currentIv.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.like));
            }
        } else {
            ToatUtils.toastCommentFail(getContext(), model.msg);
        }

    }

    @Override
    public void subSuccess(SubResultModel model) {
        hideSendingDialog();
        if (model == null) return;
        if (IsSuccessUtils.isSuccess(model.result)) {
            tv_is_sub.setText("已订阅");
            tv_is_sub.setEnabled(false);
            ToatUtils.toastSuccess(getContext(), "订阅成功");
        } else {
            ToatUtils.toastCommentFail(getContext(), "" + model.msg);
        }
    }

    @Override
    public void getShareDataSuccess(ShareModel model) {
        dismissShare();
        if (model == null || model.data == null) return;
        if (IsSuccessUtils.isSuccess(model.result)) {
            //  AppUtils.check(getActivity());
            UMImage thumb = new UMImage(getActivity(), model.data.cover_path);
            UMWeb web = new UMWeb(model.data.url);
            web.setTitle(model.data.title);
            web.setDescription(model.data.des);
            web.setThumb(thumb);

            switch (currentShareType) {
                case ShareDialog.TYPE_WX:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN).withText(comeFrom).withMedia(web)
                            .setCallback(umShareListener).share();
                    break;
                case ShareDialog.TYPE_WX_CIRCLE:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).withText(comeFrom).setCallback(umShareListener).withMedia(web).share();
                    break;
                case ShareDialog.TYPE_QQ:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QQ).withText(comeFrom).withMedia(web).setCallback(umShareListener).share();
                    break;
                case ShareDialog.TYPE_QQ_ZONE:
                    new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QZONE).withText(comeFrom).withMedia(web).setCallback(umShareListener).share();
                    break;
                case ShareDialog.TYPE_COLLECTION:
                    presenter.collcetion(getCPJson(caid));
                    break;
                case ShareDialog.TYPE_REPORT:
                    //TODO

                    break;
                case ShareDialog.TYPE_DISMISS:
                    dismissShare();
                    break;
            }
        } else {
            ToatUtils.toastSuccess(getContext(), model.msg);
        }
    }

    @Override
    public void collectionSuccess(CollectionRModel model) {
        dismissShare();
        if (model == null) return;
        if (model.money_msg != null) {
            ToatUtils.toastCommentSuccess(getContext(), "完成" + model.money_msg.info + "\n获取" + model.money_msg.money + "金币");
        }
        if (IsSuccessUtils.isSuccess(model.result)) {
            ToatUtils.toastSuccess(getContext(), "收藏成功");
        } else {
            ToatUtils.toastSuccess(getContext(), "" + model.msg);
        }
    }

    @Override
    public void weakSuccess(ResultModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                try {
                    if (!StringUtils.isW(tv_detail_weak_count.getText().toString())) {
                        int count = Integer.parseInt(tv_detail_weak_count.getText().toString()) + 1;
                        tv_detail_weak_count.setText(count + "");
                    }
                } catch (NumberFormatException e) {

                }

            } else {
                ivDetailWeak.setClickable(false);
                ivDetailWeak.setEnabled(false);
                ToatUtils.toastSuccess(getContext(), model.msg);
            }
        }
    }

    @Override
    public void zanSuccess(ResultModel model) {
        if (model != null) {
            if (model.money_msg != null) {
                ToatUtils.toastCommentSuccess(getContext(), "完成" + model.money_msg.info + "\n获取" + model.money_msg.money + "金币");
                AppUtils.refreshMe();
            }
            if (IsSuccessUtils.isSuccess(model.result)) {
                try {
                    if (!StringUtils.isW(tv_detail_zan_count.getText().toString())) {
                        int count = Integer.parseInt(tv_detail_zan_count.getText().toString()) + 1;
                        tv_detail_zan_count.setText(count + "");
                    }
                } catch (NumberFormatException e) {

                }
            } else {
                ivDetailZan.setClickable(false);
                ivDetailZan.setEnabled(false);
                ToatUtils.toastSuccess(getContext(), model.msg);
            }
        }
    }

    @Override
    public void shareSuccess(ResultModel model) {
        if (model != null) {
            if (model.money_msg != null) {
                ToatUtils.toastCommentSuccess(getContext(), "完成" + model.money_msg.info + "\n获取" + model.money_msg.money + "金币");
                AppUtils.refreshMe();
            }
        }
    }

    private String getCPJson(String cid) {
        CollcetionPModel model = new CollcetionPModel();
        model.cid = cid;
        model.type = 1;
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        String time = System.currentTimeMillis() / 1000 + "";
        model.time = time;
        model.authcode = Cutils.getInstance().getCode();
        model.md5authcode = DescriptUtils.SHA1(time + model.authcode + model.authcode + ConstantUtils.SHA1);
        return new Gson().toJson(model);
    }

    public String getZanJson(String coid) {
        CommentZanModel model = new CommentZanModel();
        model.coid = coid;
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        String json = new Gson().toJson(model);
        return json;
    }

    private int dip2px(float dpValue) {
        final float scale = getActivity().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    void initViews() {
        loadingView = (RelativeLayout) contentView.findViewById(R.id.rl_loading);
        containerView = (RelativeLayout) contentView.findViewById(R.id.rl_container);
        progressBar = (ProgressBar) contentView.findViewById(R.id.pb_loading);
        loadingView.setVisibility(View.VISIBLE);
        containerView.setVisibility(View.GONE);
        tv_is_sub = (TextView) contentView.findViewById(R.id.tv_is_sub);
        tv_detail_zan_count = (TextView) contentView.findViewById(R.id.tv_detail_zan_count);
        tv_com_times = (TextView) contentView.findViewById(R.id.tv_com_times);
        tv_a_detail_title = (TextView) contentView.findViewById(R.id.tv_a_detail_title);
        tv_a_detail_name = (TextView) contentView.findViewById(R.id.tv_a_detail_name);
        tv_a_detail_name.setVisibility(View.GONE);
        tv_a_detail_time = (TextView) contentView.findViewById(R.id.tv_a_detail_time);
        tv_detail_weak_count = (TextView) contentView.findViewById(R.id.tv_detail_weak_count);
        tv_detail_ca_name = (TextView) contentView.findViewById(R.id.tv_detail_ca_name);
        tv_hot_comment_tips = (TextView) contentView.findViewById(R.id.tv_hot_comment_tips);
        iv_user_p = (CircleImageView) contentView.findViewById(R.id.iv_user_p);
        v_hot_comment = contentView.findViewById(R.id.v_hot_comment);
        iv_share_detail = (ImageView) contentView.findViewById(R.id.iv_share_detail);
        iv_a_detail_title_pic = (ImageView) contentView.findViewById(R.id.iv_a_detail_title_pic);
        iv_share_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();
            }
        });
        rl_to_comment = (RelativeLayout) contentView.findViewById(R.id.rl_to_comment);
        rl_detail_comment_more = (RelativeLayout) contentView.findViewById(R.id.rl_detail_comment_more);
        rl_detail_comment_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startComment(getActivity(), caid);
            }
        });

        ll_container_detail_two = (LinearLayout) contentView.findViewById(R.id.ll_container_detail_two);
        ll_more_comment = (LinearLayout) contentView.findViewById(R.id.ll_more_comment);
        ll_more_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startComment(getActivity(), caid);
            }
        });
        wvDetail = (WebView) contentView.findViewById(R.id.wv_detail);

        setWebView(wvDetail);
        wvDetail.setWebChromeClient(new MyWebChrome());
        wvDetail.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                view.loadUrl("javascript:(function(){" +
                        "var objs = document.getElementsByTagName(\"img\"); " +
                        "var url = new Array(); " +
                        "for(var j = 0; j < objs.length; j++)" +
                        "{" +
                        "url[j]=objs[j].src" +
                        "}" +
                        "for(var i=0;i<objs.length;i++)  " +
                        "{"
                        + "    objs[i].onclick=function()  " +
                        "    {  "
                        + "        window.wzf_image_src.showSource(url,this.src);  " +
                        "    }  " +
                        "}" +
                        "})()");
//                view.loadUrl("javascript:window.wzf_image_src.showHtml('<html>'+"
//                        + "document.getElementsByTagName('html')[0].innerHTML+'</html>');");
            }
        });
        wvDetail.loadUrl(UrlConstant.WebViewUrl + caid);

        ivDetailZan = (ImageView) contentView.findViewById(R.id.iv_detail_zan);
        rl_zan_container = (RelativeLayout) contentView.findViewById(R.id.rl_zan_container);
        rl_detail_return = (RelativeLayout) contentView.findViewById(R.id.rl_detail_return);
        rl_name_de = (RelativeLayout) contentView.findViewById(R.id.rl_name_de);
        rl_detail_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(getActivity());
            }
        });

        ivDetailZan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  if (count >= 10) return;
                if (!SharePreferencesUtils.isLogin()) {
                    showIsLogin();
                    return;
                }
                com.tjp.eight.utils.AnimationUtils.ScalView(ivDetailZan);
                startCount();
                //count++;
                presenter.azan(getPJson("praise"));
            }
        });
        ivDetailWeak = (ImageView) contentView.findViewById(R.id.iv_detail_weak);

        ivDetailWeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // if (count2 >= 10) return;
                if (!SharePreferencesUtils.isLogin()) {
                    showIsLogin();
                    return;
                }
                com.tjp.eight.utils.AnimationUtils.ScalView(ivDetailWeak);
                weakCount();
                presenter.weak(getPJson("unpraise"));
                //count2++;
            }
        });

        ll_hot_comment = (LinearLayout) contentView.findViewById(R.id.ll_hot_comment);
        ll_like_container = (LinearLayout) contentView.findViewById(R.id.ll_like_container);

        tv_detail_comment_click = (TextView) contentView.findViewById(R.id.tv_detail_comment_click);
        tv_detail_comment_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCommentDialog();
            }
        });
        ll_history_one = (LinearLayout) contentView.findViewById(R.id.ll_history_one);
        ll_history_two = (LinearLayout) contentView.findViewById(R.id.ll_history_two);
    }


    class InJavaScriptImageSrc {
        @JavascriptInterface
        public void showSource(String[] src, String positionSrc) {
            int position = 0;
            ImageModel model = new ImageModel();
            for (String p : src) {
                if (p.equals(positionSrc)) {
                    model.postion = position;
                }
                position++;
            }
            model.length = src.length;
            model.src = AppUtils.getUrls(src);
            String json = new Gson().toJson(model);
            Intent intent = new Intent(getActivity(), ImageViewActivity.class);
            intent.putExtra("json", json);
            startActivity(intent);
        }

        @JavascriptInterface
        public void showHtml(String html) {
            Log.e("html", "" + html);
        }
    }

    void setWebView(WebView view) {
        view.getSettings().setSupportZoom(false);
        view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setDefaultTextEncodingName("UTF-8");
        view.getSettings().setAppCacheEnabled(false);
        view.getSettings().setSaveFormData(true);// 保存表单数据
        view.addJavascriptInterface(new InJavaScriptImageSrc(), "wzf_image_src");
        view.getSettings().setGeolocationEnabled(true);// 启用地理定位
        view.getSettings().setDomStorageEnabled(true);
        view.getSettings().setSupportMultipleWindows(true);// 新加
        switch (SharePreferencesUtils.geWebTextSize()) {
            case 2:
                view.getSettings().setTextZoom(75);
                break;
            case 3:
                view.getSettings().setTextZoom(100);
                break;
            case 4:
                view.getSettings().setTextZoom(150);
                break;
            default:
                view.getSettings().setTextZoom(100);
                break;
        }
    }

    void showShare() {
        if (shareDialog == null) {
            shareDialog = new ShareDialog(getContext());
            shareDialog.setOnClickType(new ShareDialog.OnClickType() {
                @Override
                public void clickType(int type) {
                    currentShareType = type;
                    presenter.getShareData(getShareJson());
                    switch (type) {
                        case ShareDialog.TYPE_WX:
                            break;
                        case ShareDialog.TYPE_WX_CIRCLE:
                            break;
                        case ShareDialog.TYPE_QQ:
                            break;
                        case ShareDialog.TYPE_QQ_ZONE:
                            break;
                        case ShareDialog.TYPE_COLLECTION:
                            break;
                        case ShareDialog.TYPE_REPORT:
                            break;
                        case ShareDialog.TYPE_DISMISS:
                            dismissShare();
                            break;
                    }
                }
            });
        }
        shareDialog.show();
    }

    private String getShareJson() {
        SharePostModel model = new SharePostModel();
        model.cid = caid;
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(model);
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            ToatUtils.toastSuccess(getContext(), "分享成功");
            presenter.shareSuccess(getShareRJson());
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            if (throwable != null)
                ToatUtils.toastSuccess(getContext(), "分享失败！由" + throwable.getMessage() + "导致");
            else
                ToatUtils.toastSuccess(getContext(), "分享失败！");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            ToatUtils.toastSuccess(getContext(), "用户取消分享");
        }
    };

    private String getShareRJson() {
        SharePostModel model = new SharePostModel();
        model.cid = caid;
        model.type = "1";
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.authcode = Cutils.getInstance().getCode();
        model.time = System.currentTimeMillis() / 1000 + "";
        model.md5authcode = DescriptUtils.SHA1(model.time + model.authcode + model.authcode + ConstantUtils.SHA1);
        return new Gson().toJson(model);
    }

    void dismissShare() {
        if (shareDialog != null) {
            shareDialog.dismiss();
        }
    }

    void showSendingDialog() {
        if (dialogSendingComment == null) {
            dialogSendingComment = new DialogSendingComment(getActivity());
        }
        dialogSendingComment.show();
    }

    void hideSendingDialog() {
        if (dialogSendingComment == null) {
            dialogSendingComment = new DialogSendingComment(getActivity());
        }
        dialogSendingComment.dismiss();
    }

    void showIsLogin() {
        if (dialogLogin == null) {
            dialogLogin = new LogoutDailog(getActivity());
        }
        dialogLogin.setTextTitle("亲！你还没登录呢");
        dialogLogin.setLogoutInterface(new LogoutDailog.LogoutInterface() {
            @Override
            public void logout() {
                ActivityUtils.startLoginForResult(getActivity());
                dialogLogin.dismiss();
            }
        });
        dialogLogin.setCancelText("再去逛逛");
        dialogLogin.setOkText("去登录");
        dialogLogin.show();
    }

    void showCommentDialog() {
        if (!SharePreferencesUtils.isLogin()) {
            showIsLogin();
            return;
        }
        if (sendCommentDialog == null) {
            sendCommentDialog = new SendCommentDialog(getContext());
        }
        rl_to_comment.setVisibility(View.INVISIBLE);
        sendCommentDialog.show();
        sendCommentDialog.setSendComment(new SendCommentDialog.SendComment() {
            @Override
            public void sendComment(String msg) {
                sendCommentDialog.dismiss();
                if (StringUtils.isNullOrEmpty(msg)) return;
                presenter.sendComment(getCommentJson(msg));

                showSendingDialog();
            }
        });
        sendCommentDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rl_to_comment.setVisibility(View.VISIBLE);
                    }
                }, 100);
            }
        });
    }

    private String getCommentJson(String msg) {
        SendCommentPostModel postModel = new SendCommentPostModel();
        postModel.cid = caid;
        postModel.detail = msg;
        postModel.uid = SharePreferencesUtils.getUid();
        postModel.loginkey = SharePreferencesUtils.getLoginKey();

        String json = new Gson().toJson(postModel);
        return json;
    }

    void initHotComment(final ArrayList<CommentModel> commentModels) {
        if (commentModels == null || commentModels.isEmpty()) {
            v_hot_comment.setVisibility(View.GONE);
            tv_hot_comment_tips.setText("暂无评论，赶紧来抢沙发吧！");
            return;
        }
        v_hot_comment.setVisibility(View.VISIBLE);
        tv_hot_comment_tips.setText("查看更多评论");
        ll_hot_comment.removeAllViews();
        for (int i = 0; i < commentModels.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.hot_comment_item, null);
            ImageView civ_u_pic_hot_comment = (ImageView) view.findViewById(R.id.civ_u_pic_hot_comment);
            LinearLayout ll_detail_zan = (LinearLayout) view.findViewById(R.id.ll_detail_zan);
            final ImageView iv_zan = (ImageView) view.findViewById(R.id.iv_zan);
            TextView tv_hot_comment_name = (TextView) view.findViewById(R.id.tv_hot_comment_name);
            TextView tv_hot_comment_time = (TextView) view.findViewById(R.id.tv_hot_comment_time);
            TextView tv_hot_comment_content = (TextView) view.findViewById(R.id.tv_hot_comment_content);
            final TextView tv_hot_comment_zan = (TextView) view.findViewById(R.id.tv_hot_comment_zan);
            TextView tv_hot_comment_repeat = (TextView) view.findViewById(R.id.tv_hot_comment_repeat);
            Glide.with(getContext()).load(commentModels.get(i).iconurl).error(R.drawable.icon_login).into(civ_u_pic_hot_comment);
            if (IsSuccessUtils.isSero(commentModels.get(i).if_praise)) {
                iv_zan.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.like_un));
            } else {
                iv_zan.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.like));
            }
            final String coid = commentModels.get(i).coid;
            ll_detail_zan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点赞回调等接口。。。。
                    //TODO
                    if (!SharePreferencesUtils.isLogin()) {
                        showIsLogin();
                        return;
                    }
                    presenter.zanClick(getZanJson(coid));
                    currentTv = tv_hot_comment_zan;
                    currentIv = iv_zan;
                }
            });
            try {
                new EmojiUtil().handlerEmojiText(tv_hot_comment_content, commentModels.get(i).detail, getActivity());
            } catch (IOException e) {
                e.printStackTrace();
            }
            tv_hot_comment_name.setText(commentModels.get(i).name);
            tv_hot_comment_time.setText(commentModels.get(i).pass_time);
            tv_hot_comment_zan.setText("" + StringUtils.toWan(commentModels.get(i).praise));
            tv_hot_comment_repeat.setText("" + StringUtils.toWan(commentModels.get(i).com_time));
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityUtils.startComment(getActivity(), caid);
                }
            });
            ll_hot_comment.addView(view);
        }
    }

    void initYouLike(final ArrayList<DetailModel.LikeDetailModel> model) {
        if (model == null || model.isEmpty()) return;
        ll_like_container.removeAllViews();
        for (int i = 0; i < model.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.y_like_item, null);
            TextView tv_like_title = (TextView) view.findViewById(R.id.tv_like_title);
            TextView tv_like_c_name = (TextView) view.findViewById(R.id.tv_like_c_name);
            TextView tv_like_views = (TextView) view.findViewById(R.id.tv_like_views);
            ImageView iv_sub_pic = (ImageView) view.findViewById(R.id.iv_sub_pic);
            Glide.with(getContext()).load(model.get(i).cover_path).error(getResources().getDrawable(R.drawable.loading)).into(iv_sub_pic);
            tv_like_c_name.setText(model.get(i).caid_name);
            tv_like_title.setText(model.get(i).title);
            tv_like_views.setText(StringUtils.toWan(model.get(i).views_all) + "");
            final String caidy = model.get(i).cid;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ActivityUtils.startDetails(getActivity(), caidy);
                }
            });
            ll_like_container.addView(view);
        }
    }

    private void initHistory(final ArrayList<HistoryModel> arrayList, int count) {
        int size = 0;
        if (arrayList != null || !arrayList.isEmpty()) {
            size = arrayList.size();
        }
        ll_history_two.removeAllViews();
        ll_history_one.removeAllViews();

        for (int i = 0; i < 12; i++) {
            if (i == 11) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.foot_wall_item_num, null, false);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
                TextView tv_history = (TextView) view.findViewById(R.id.tv_history);
                if (count > 99)
                    tv_history.setText("99+");
                else
                    tv_history.setText("" + count);
                view.setLayoutParams(layoutParams);
                ll_history_two.addView(view);
                tv_history.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //足迹墙
                        Intent intent = new Intent(getActivity(), ActivityZuJi.class);
                        intent.putExtra("cid", caid);
                        startActivity(intent);
                    }
                });
            } else {

                View view = LayoutInflater.from(getContext()).inflate(R.layout.foot_wall_item_pic, null, false);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
                CircleImageView civ_history = (CircleImageView) view.findViewById(R.id.civ_history);

                view.setLayoutParams(layoutParams);
                if (i < size) {
                    Glide.with(getActivity()).load(arrayList.get(i).iconurl).error(R.drawable.icon_login).into(civ_history);
                    civ_history.setVisibility(View.VISIBLE);
                    final String uid = arrayList.get(i).uid;
                    civ_history.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ActivityUtils.startOtherHomePage(getActivity(), uid);
                        }
                    });
                } else {
                    Glide.with(getContext()).load("").error(R.drawable.icon_login).into(civ_history);
                    civ_history.setVisibility(View.INVISIBLE);
                }
                if (i < 6) {
                    ll_history_one.addView(view);
                } else {
                    ll_history_two.addView(view);
                }
            }

        }
    }

    void startCount() {
        // int count = Integer.parseInt(tv_detail_zan_count.getText().toString()) + 1;
        //tv_detail_zan_count.setText(count + "");
        View textView = LayoutInflater.from(getContext()).inflate(R.layout.add_one, null, false);
        TextView textView1 = (TextView) textView.findViewById(R.id.tv_add_);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(screenWidth / 4 + dip2px(3), dip2px(64), 0, 0);
        textView1.setLayoutParams(layoutParams);
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.add_score_anim);
        animation.setAnimationListener(new MyAni(textView));

        rl_zan_container.addView(textView);
        textView.startAnimation(animation);
    }

    private String getPJson(String act) {
        DetailZWModel model = new DetailZWModel();

        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.act = act;
        model.cid = caid;

        return new Gson().toJson(model);
    }

    private String getPostJson() {
        DetailPostModel detailPostModel = new DetailPostModel();
        detailPostModel.cid = caid;
        detailPostModel.uid = SharePreferencesUtils.getUid();
        detailPostModel.loginkey = SharePreferencesUtils.getLoginKey();

        String json = new Gson().toJson(detailPostModel);

        return json;
    }

    void weakCount() {
//        int count = Integer.parseInt(tv_detail_weak_count.getText().toString()) + 1;
//        tv_detail_weak_count.setText(count + "");
        View textView = LayoutInflater.from(getContext()).inflate(R.layout.weak_one, null, false);
        TextView textView1 = (TextView) textView.findViewById(R.id.tv_weak_);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, dip2px(64), screenWidth / 4 - dip2px(20), 0);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        textView1.setLayoutParams(layoutParams);
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.add_score_anim);
        animation.setAnimationListener(new MyAni(textView));

        rl_zan_container.addView(textView);
        textView.startAnimation(animation);
    }

    private void initHeight() {
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        screenHeight = point.y;
        screenWidth = point.x;

        keyBordHeight = screenHeight / 3;
    }

    private String getSubJson(String caid) {
        SubPModel pmodel = new SubPModel();
        pmodel.caid = caid;
        pmodel.uid = SharePreferencesUtils.getUid();
        pmodel.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(pmodel);
    }

    class MyAni implements Animation.AnimationListener {
        public MyAni(View textView) {
            this.textView = textView;
        }

        View textView;

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (textView != null) textView.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }

    class MyWebChrome extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
        }
    }

}
